<?php
/**
 * 系统函数
 * Created by PhpStorm.
 * User: simon_min@aliyun.com
 * Date: 2016/11/5
 * Time: 12:45
 */

/**
 * 错误提示函数
 * @param $error 错误信息
 * @param string $level 错误级别，默认ERROR
 * @param int $type 错误类型，默认为3
 * @param null $dest 日志存放目录
 */
function halt($error, $level = 'ERROR', $type = 3, $dest = NULL)
{
    if (is_array($error)) {
        Log::write($error['message'], $level, $type, $dest);
    } else {
        Log::write($error, $level, $type, $dest);
    }
    $e = array();
    //开启DEBUG的操作
    if (DEBUG) {
        if (!is_array($error)) {
            $trace = debug_backtrace();
            $e['message'] = $error;
            $e['file'] = $trace[0]['file'];
            $e['line'] = $trace[0]['line'];
            $e['class'] = isset($trace[0]['class']) ? $trace[0]['class'] : '';
            $e['function'] = isset($trace[0]['function']) ? $trace[0]['function'] : '';
            ob_start();
            debug_print_backtrace();
            $e['trace'] = htmlspecialchars(ob_get_clean());
        } else {
            $e = $error;
        }
    } else {
        if ($url = C('ERROR_URL')) {
            jump($url);
        } else {
            $e['message'] = C('ERROR_MSG');
        }
    }
    include DATA_PATH . '/Tpl/halt.html';
    die;
}

/**
 * 打印函数
 * @param $arr
 */
function dump($var, $echo = true, $label = null, $strict = true)
{
    $label = ($label === null) ? '' : rtrim($label) . ' ';
    if (!$strict) {
        if (ini_get('html_errors')) {
            $output = print_r($var, true);
            $output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
        } else {
            $output = $label . print_r($var, true);
        }
    } else {
        ob_start();
        var_dump($var);
        $output = ob_get_clean();
        if (!extension_loaded('xdebug')) {
            $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        }
    }
    if ($echo) {
        echo($output);
        return null;
    } else
        return $output;
}

/**
 * 跳转函数
 * @param $url 跳转的地址
 * @param int $time 跳转时间，默认立即跳转
 * @param string $msg 提示信息，默认空字符串
 */
function jump($url, $time = 0, $msg = '')
{
    if (!headers_sent()) {
        $time == 0 ? header('Location:' . $url) : header("refresh:{$time},url={$url}");
        die($msg);
    } else {
        echo "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
        if ($time) die($msg);
    }
}

/**
 * 加载配置项,读取配置项,临时动态改变配置项
 */
function C($var = NULL, $value = NULL)
{
    static $config = array();
    //加载配置项
    if (is_array($var)) {
        $config = array_merge($config, array_change_key_case($var, CASE_UPPER));
        return;
    }
    //读取，动态改变配置项
    if (is_string($var)) {
        $var = strtoupper($var);
        //两个参数传递
        if (!is_null($value)) {
            $config[$var] = $value;
            return;
        }
        return isset($config[$var]) ? $config[$var] : NULL;
    }
    //返回所有配置项
    if (is_null($var) && is_null($value)) {
        return $config;
    }
}

/**
 * 打印常量
 */
function print_const()
{
    $const = get_defined_constants(true);
    dump($const['user']);
}

/**
 * M方法
 * @param $table
 * @return Model
 */
function M($table)
{
    $obj = new Model($table);
    return $obj;
}

/**
 * K方法，用户自定义模型实例化
 */
function K($model)
{
    $model .= 'Model';
    return new $model;
}




















